<!--个人信息修改页面-->
<template>
    <div>
        <el-card style="width: 50%;margin:auto">
            <el-form :model="user" :rules="rules" label-width="80px" style="padding-right: 20px">
                <div style="margin: 15px; text-align: center">
                    <el-upload
                            class="avatar-uploader"
                            action="http://localhost:9090/file/uploadavatar"
                            :headers="{ token: user.token }"
                            :show-file-list="false"
                            :on-success="handleAvatarSuccess"
                            :before-upload="beforeAvatarUpload"
                    >
                        <img v-if="user.avatar" :src="user.avatar" class="avatar"/>
                        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                    </el-upload>
                </div>
                <el-form-item label="账号" prop="userid">
                    <el-input v-model="user.userid" disabled></el-input>
                </el-form-item>
                <el-form-item label="姓名" prop="username">
                    <el-input v-model="user.username"></el-input>
                </el-form-item>
                <el-form-item label="电话" prop="phone">
                    <el-input v-model="user.phone"></el-input>
                </el-form-item>
                <el-form-item label="学部" prop="major" >
                    <el-select v-model="user.major" placeholder="请选择学部">
                        <el-option
                                v-for="item in options"
                                :key="item"
                                :label="item"
                                :value="item">
                        </el-option>
                    </el-select>
                </el-form-item>
                <div style="text-align: center">
                    <el-button type="primary" @click="update">保 存</el-button>
                </div>
            </el-form>
        </el-card>
    </div>
</template>

<script>
    import {cellphoneValidator} from "@/utils/userPhone";

    export default {
        name: "Person",
        data() {
            return {
                user: JSON.parse(localStorage.getItem("honey-user") || '{}'),//拿取个人用户信息
                //学部
                options: [],
                rules: {
                    username:[
                        {required: true, message:'请输入姓名', trigger:'blur'},
                    ],
                    phone:[
                        {required: true, message:'请输入手机号', trigger:'blur'},
                        { validator: cellphoneValidator, trigger: 'blur' }
                    ],
                    major:[
                        {required: true, message:'请选择学部', trigger:'blur'},
                    ],
                },
            }
        },
        created() {
            //接收学部类型值
            this.$request.get('/faculty/selectAll').then(res => {
                this.options = res.data
                console.log(res)
                console.log(this.options)
            })
        },
        methods: {
            beforeAvatarUpload(file) {//限制文件上传格式和大小
                // const isJPG = file.type === 'image/jpeg';
                const testmsg = /^image\/(jpeg|png|jpg)$/.test(file.type);
                const isLt2M = file.size / 1024 / 1024 < 2;
                if (!testmsg) {
                    this.$message.error('上传头像图片格式错误!');
                }
                if (!isLt2M) {
                    setTimeout(() => {
                        this.$message.error('上传头像图片大小不能超过 2MB!');
                    }, 10);
                }
                return isLt2M && testmsg;
            },
            update() {
                //保存当前的用户信息到数据库
                this.$request.put('/user/update', this.user).then(res => {
                    if (res.code === '200') {
                        //更新成功
                        this.$router.push('/home')
                        this.$message.success('保存成功')
                    } else {
                        this.$message.error(res.msg)
                    }
                })
                //更新浏览器里的缓存信息
                localStorage.setItem('honey-user', JSON.stringify(this.user))
                //触发父级的数据更新
                this.$emit('update:user', this.user)
            },
            handleAvatarSuccess(response, file, fileList) {
                //把user的头像属性换成上传的头像链接
                this.user.avatar = response.data
                console.log(response.data)
            }
        }
    }

</script>

<style scoped>
    /deep/ .el-form-item__label {
        font-weight: bold;
    }  /* /deep/:穿透 elementui */

    /deep/ .el-upload {
        border-radius: 50%;
    }

    /deep/.avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 50%;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }

    /deep/.avatar-uploader .el-upload:hover {
        border-color: #409EFF;
    }

    .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 178px;
        height: 178px;
        line-height: 178px;
        text-align: center;
        border-radius: 50%;
    }

    .avatar {
        width: 178px;
        height: 178px;
        display: block;
        border-radius: 50%;
    }
</style>